<?php
/**
 *    实名认证控制器
 *
 *    @author    yanghj
 *    @time   2014-06-4
 */
class CertificationApp extends BackendApp
{
	var $_cert_mod;

	function __construct()
	{
		$this->PartnerApp();
	}

	function PartnerApp()
	{
		parent::BackendApp();

		$this->_cert_mod =& m('certification');
	}

	/**
     *    管理
     *
     *    @author    yanghj
     *    @time   2014-06-4
     *    @return    void
     */
	function index()
	{

		$search_options = array(
        'user_name' => '用户名',
		'real_name'   => '真实姓名',
        'identity_card'   => '身份证号',
		'company_name'   => '企业名称',       
        'license_card'   => '营业执照注册号',
		);
        $status_list = array(
        '0' => '等待审核',
		'1'   => '审核成功',
        '-1'   => '审核失败',
		);
        $certification_type = array(
        '1' => '个人认证',
		'2'   => '企业认证',
		);
        
		/* 默认搜索的字段是真实姓名 */
		$field = 'user_name';
		array_key_exists($_GET['field'], $search_options) && $field = $_GET['field'];
		$conditions = $this->_get_query_conditions(array(array(
		'field' => $field,       
		'equal' => 'LIKE',
		'name'  => 'search_name',
		),array(
		'field' => 'certification_type',
		'equal' => '=',
		'type'  => 'numeric',
		),array(
		'field' => 'status',
		'equal' => '=',
		'type'  => 'numeric',
		),array(
		'field' => 'add_time',
		'name'  => 'add_time_from',
		'equal' => '>=',
		'handler'=> 'gmstr2time',
		),array(
		'field' => 'add_time',
		'name'  => 'add_time_to',
		'equal' => '<=',
		'handler'   => 'gmstr2time_end',
		),
		));

		$page   =   $this->_get_page(10);    //获取分页信息
		//更新排序
		if (isset($_GET['sort']) && isset($_GET['order']))
		{
			$sort  = strtolower(trim($_GET['sort']));
			$order = strtolower(trim($_GET['order']));
			if (!in_array($order,array('asc','desc')))
			{
				$sort  = 'add_time';
				$order = 'desc';
			}
		}
		else
		{
			$sort  = 'add_time';
			$order = 'desc';
		}
		$data_list = $this->_cert_mod->find(array(
		'conditions'    => '1=1 ' . $conditions,
		'limit'         => $page['limit'],  //获取当前页的数据
		'order'         => "$sort $order",
		'count'         => true             //允许统计
		));
		$page['item_count'] = $this->_cert_mod->getCount();   //获取统计的数据
		$this->_format_page($page);
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->assign('certification_type', $certification_type);//认证类型
		$this->assign('status_list', $status_list);//审核状态
		$this->assign('search_options', $search_options);
		$this->assign('page_info', $page);          //将分页信息传递给视图，用于形成分页条
		$this->assign('data_list', $data_list);
		$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
		'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
		$this->display('certification.index.html');

	}

	/**
     *    编辑
     *    @author    yanghj
     *    @time   2014-06-4
     */
	function edit()
	{
	   $status_list = array(
        '0' => '等待审核',
		'1'   => '审核成功',
        '-1'   => '审核失败',
		);
        $certification_type = array(
        '1' => '个人认证',
		'2'   => '企业认证',
		);
		$cert_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if (!$cert_id)
		{
			$this->show_warning('没有记录');

			return;
		}
		include(ROOT_PATH.'/includes/cfg_status.php');
		if (!IS_POST)
		{
			$find_data     = $this->_cert_mod->find($cert_id);
			if (empty($find_data))
			{
				$this->show_warning('没有记录');

				return;
			}
			$cert_info    =   current($find_data);
            $cert_info['identity_img1']    =   dirname(site_url()) . '/' . $cert_info['identity_img1'];
            $cert_info['identity_img2']    =   dirname(site_url()) . '/' . $cert_info['identity_img2'];
            $cert_info['license_img']    =   dirname(site_url()) . '/' . $cert_info['license_img'];
            $cert_info['bank_name']    =   $cfg_bank_name[$cert_info['bank_name']];
			$this->assign('cert', $cert_info);
			$this->import_resource('jquery.plugins/jquery.validate.js');
            $this->assign('certification_type', $certification_type);//认证类型
	    	$this->assign('status_list', $status_list);//审核状态
			$this->display('certification.form.html');
		}
		else
		{
			$data = array();
			$data['status']      =   intval($_POST['status']);
			$data['admin_name']       =  $_SESSION['admin_info']['user_name'];
			$data['description'] =   html_script($_POST['admin_note']);//操作备注
            $data['audit_time'] = gmtime();           
            $user_id = intval($_POST['user_id']);
            if($data['status'] == 0)
            {
               $this->show_warning('请选择审核结果!');
				return; 
            }
			$rows = $this->_cert_mod->edit($cert_id, $data);
			if ($this->_cert_mod->has_error())    //有错误
			{
				$this->show_warning($this->_cert_mod->get_error());

				return;
			}
            if($data['status'] == 1)
            {
                $user_safety_mod = &m('usersafety');
                $user_safety_mod->add_edit(array('user_id' =>$user_id, 'is_certificate' => 1), $user_id);
                if ($user_safety_mod->has_error()) {
                    $this->show_warning($user_safety_mod->get_error());      
                    return;
                }
                
            }
            

			$this->admin_log('审核实名认证ID-'.$cert_id.':'.$status_list[$data['status']]);

			$this->show_message('审核成功',
			'back_list',     'index.php?app=certification');
		}
	}


}

?>